package com.ruoyi.recruit.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.recruit.domain.RecruitContent;
import com.ruoyi.recruit.domain.RecruitUser;
import com.ruoyi.recruit.domain.RecruitUserConfirm;
import com.ruoyi.recruit.mapper.RecruitContentMapper;
import com.ruoyi.recruit.mapper.RecruitUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.recruit.mapper.RecruitUserInterviewMapper;
import com.ruoyi.recruit.domain.RecruitUserInterview;
import com.ruoyi.recruit.service.IRecruitUserInterviewService;
import com.ruoyi.common.core.text.Convert;

/**
 * 面试人员成绩Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-05
 */
@Service
public class RecruitUserInterviewServiceImpl extends ServiceImpl<RecruitUserInterviewMapper, RecruitUserInterview> implements IRecruitUserInterviewService
{
    @Autowired
    private RecruitContentMapper recruitContentMapper;

    @Autowired
    private RecruitUserMapper recruitUserMapper;

    /**
     * 查询面试人员成绩
     * 
     * @param id 面试人员成绩主键
     * @return 面试人员成绩
     */
    @Override
    public RecruitUserInterview selectRecruitUserInterviewById(Long id)
    {
        return baseMapper.selectRecruitUserInterviewById(id);
    }

    /**
     * 查询面试人员成绩列表
     * 
     * @param recruitUserInterview 面试人员成绩
     * @return 面试人员成绩
     */
    @Override
    public List<RecruitUserInterview> selectRecruitUserInterviewList(RecruitUserInterview recruitUserInterview)
    {
        return baseMapper.selectRecruitUserInterviewList(recruitUserInterview);
    }

    /**
     * 新增面试人员成绩
     * 
     * @param recruitUserInterview 面试人员成绩
     * @return 结果
     */
    @Override
    public int insertRecruitUserInterview(RecruitUserInterview recruitUserInterview)
    {
        recruitUserInterview.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(recruitUserInterview);
    }

    /**
     * 修改面试人员成绩
     * 
     * @param recruitUserInterview 面试人员成绩
     * @return 结果
     */
    @Override
    public int updateRecruitUserInterview(RecruitUserInterview recruitUserInterview)
    {
        recruitUserInterview.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(recruitUserInterview);
    }

    /**
     * 批量删除面试人员成绩
     * 
     * @param ids 需要删除的面试人员成绩主键
     * @return 结果
     */
    @Override
    public int deleteRecruitUserInterviewByIds(String ids)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(ids)));
    }

    /**
     * 删除面试人员成绩信息
     * 
     * @param id 面试人员成绩主键
     * @return 结果
     */
    @Override
    public int deleteRecruitUserInterviewById(Long id)
    {
        return baseMapper.deleteById(id);
    }

    /**
     * 导入面试人员信息
     *
     * @param userInterviewList 面试人员信息
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importUserInterview(List<RecruitUserInterview> userInterviewList, Long recruitId, boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(userInterviewList) || userInterviewList.size() == 0)
        {
            throw new ServiceException("导入面试人员信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RecruitUserInterview userInterview : userInterviewList)
        {
            int row = 1;
            try
            {
                userInterview.setRecruitId(recruitId);
                // 获取招聘计划
                RecruitContent recruitContent = recruitContentMapper.selectOne(new LambdaQueryWrapper<RecruitContent>()
                        .eq(RecruitContent::getPostCode, userInterview.getPostCode())
                        .eq(RecruitContent::getPostName, userInterview.getPostName())
                        .eq(RecruitContent::getRecruitId, recruitId).last("limit 1"));
                if(StringUtils.isNull(recruitContent)){
                    failureNum++;
                    failureMsg.append("<br/>" + successNum + "、第：" + row + " 行招聘计划不存在");
                    continue;
                }
                userInterview.setContentId(recruitContent.getContentId());
                // 获取应聘人员信息
                RecruitUser recruitUser = recruitUserMapper.selectOne(new LambdaQueryWrapper<RecruitUser>()
                        .eq(RecruitUser::getIdCard, userInterview.getIdCard())
                        .eq(RecruitUser::getContentId, recruitContent.getContentId()).last("limit 1"));
                if (StringUtils.isNull(recruitUser))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + successNum + "、第：" + row + " 行面试人员不存在");
                    continue;
                }
                userInterview.setUserId(recruitUser.getUserId());
                //查询是否已存在到岗人员信息
                RecruitUserInterview info = baseMapper.selectOne(new LambdaQueryWrapper<RecruitUserInterview>()
                        .eq(RecruitUserInterview::getContentId, recruitContent.getContentId())
                        .eq(RecruitUserInterview::getUserId, userInterview.getUserId()).last("limit 1"));
                if(StringUtils.isNull(info)){
                    userInterview.setCreateBy(operName);
                    this.insertRecruitUserInterview(userInterview);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、第" + row + " 行面试人员信息导入成功");
                }else if(isUpdateSupport){
                    userInterview.setId(info.getId());
                    userInterview.setUpdateBy(operName);
                    this.updateRecruitUserInterview(userInterview);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、第" + row + " 行面试人员信息更新成功");
                }else{
                    failureNum++;
                    failureMsg.append("<br/>" + successNum + "、第：" + row + " 行面试人员信息已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、第" + row + " 行面试人员信息导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
            row++;
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
